package com.dgd.test.demo.controller;

import com.dgd.test.demo.bean.TeacheBean;
import com.dgd.test.demo.service.TeacherService;
import com.dgd.test.demo.vo.EduResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;


import javax.validation.Valid;
import java.util.List;


/**
 * @program: 726
 * @description:
 * @author: Jaime
 * @create: 2021-07-26 20:32
 **/
@CrossOrigin
@RestController
@RequestMapping("/edu")
public class TeacherController {
    // 注入
    @Autowired
    private TeacherService teacherService;

    //(@PathVariable 占位符  restful 风格会用
    @RequestMapping("/findTeacher/{teacherId}")
    public EduResult findTeacherById(@PathVariable Integer teacherId){
        TeacheBean teacher =  teacherService.findTeacher(teacherId);
        //return teacher;

        return EduResult.ok("").data("data",teacher );
    };


/*    //上传图片
    @PostMapping("/addTeacher")
    public EduResult addTeacher( MultipartFile file) throws IOException {
        String fileName = file.getOriginalFilename();
        file.transferTo(new File("F:\\"+fileName));
        return EduResult.ok("");
    };*/

    //ajax异步请求
    @RequestMapping("/test")
    public EduResult test(String username) {
        System.out.println("方法在这里执行");
        return EduResult.ok("").data("username",username);
    }
/*    //(@PathVariable 占位符  restful 风格会用
    @RequestMapping("/findTeacher/{teacherId}")
    //@ResponseBody
    public EduResult findTeacherById(@PathVariable Integer teacherId){
        // System.out.println("获取到的ID是"+teacherId);
        TeacheBean teacher = new TeacheBean();
        teacher.setId(teacherId);
        teacher.setName("迪丽热巴");
        teacher.setIntro("新疆");
        teacher.setLevel(1);
        teacher.setCaeer("搞不搞");
        teacher.setAvatar("2154548478");
        teacher.setDeleted(false);
        teacher.setSort(1);
        Date date = new Date();
        teacher.setGmtModified(date);
        teacher.setGmtModified(date);
        //return teacher;
        return EduResult.ok().data("data",teacher );
    };*/

 /*   //返回json格式
    @PostMapping("/addTeacher")
    public EduResult addTeacher(@Valid @RequestBody TeacheBean teacher){
            System.out.println(teacher);
        return EduResult.ok("");
    };*/

    /**
     * 添加到数据库
     */
    @PostMapping("/addTeacher")
    public EduResult addTeacher(@Valid @RequestBody TeacheBean teacher){
        int count =  teacherService.addTeacher(teacher);

        System.out.println("报错的话 不应该出现");
        return EduResult.ok("添加成功").data("count",count);
    };

    @Transactional(isolation=Isolation.READ_COMMITTED)
    @RequestMapping("/deleteTeacher")
    public EduResult deleteTeacher(@RequestBody List<Integer> ids) {
        int count = teacherService.deleteTeacher(ids);
        return EduResult.ok("删除成功").data("count",count);
    };


    @RequestMapping("/updateTeacher")
    public EduResult updateTeacher(@Valid @RequestBody TeacheBean teacher){
        int count = teacherService.updateTeacher(teacher);
        System.out.println("我们再次提交一次");
        return EduResult.ok("修改成功").data("count",count);
    }

//    加个注释测试下git
}
